package com.x;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.stereotype.Service;

/**
 * 大语言模型的服务
 * 用于创建基础的大语言模型Client，并实现Client的获取
 * @author xjk
 * @date 2025-06-16
 */
@Service
@Slf4j
public class LargeLanguageModelService {

  /**
   * 计划
   */
  private final ChatClient planningChatClient;

  /**
   * 总结
   */
  private final ChatClient summaryChatClient;


  /**
   * 对话存储
   * 最大存储1000条对话
   */
  private final ChatMemory conversationStorage = MessageWindowChatMemory.builder().maxMessages(1000).build();

  public LargeLanguageModelService(ChatModel chatModel){

    planningChatClient = ChatClient.builder(chatModel)
        .defaultAdvisors(new SimpleLoggerAdvisor())
        .defaultOptions(OllamaOptions.builder().temperature(0.1).build())
        .build();


    summaryChatClient = ChatClient.builder(chatModel)
        .defaultAdvisors(MessageChatMemoryAdvisor.builder(conversationStorage).build())
        .defaultAdvisors(new SimpleLoggerAdvisor())
        .build();


  }


  public ChatClient getPlanningChatClient(){
    return planningChatClient;
  }

  public ChatClient getSummaryChatClient() {
    return summaryChatClient;
  }
}
